
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.ImageObserver;

import javax.swing.*;
import java.net.*;

public class Mundo extends Applet implements Runnable,MouseListener,MouseMotionListener
{
private Image dbImage;
private Graphics dbg;
private Image bGround;
private Partes pc1;
private Partes pc2;
private Partes pc3;
private Partes pc4;
private Partes pc5;
private Partes pc6;
private Partes pc7;
private Partes pc8;

private Mapeamento mapCentral;
private Botao btn;



private Image pc1_img;
private Image pc2_img;
private Image pc3_img;
private Image pc4_img;
private Image pc5_img;
private Image pc6_img;
private Image pc7_img;
private Image pc8_img;

private Image map_img;
private Image btn_img;

private GameState currentState;

public Mundo()
{

}

public enum GameState
{
stateStoped,
statePlaying;
}

public void setcurrentStatePlay()
{
currentState = GameState.statePlaying;
}

public void setcurrentStateStop()
{
currentState = GameState.stateStoped;
}

public void init()
{
this.setSize(760,420);
MediaTracker mt = new MediaTracker(this);
bGround = getImage(getCodeBase(),"Cade2[1].gif");
mt.addImage(bGround,0);
try
{
mt.waitForID(0);
}
catch (InterruptedException ie)
{
//faz nda
}


mapCentral = new Mapeamento("peca-1.gif",520, 68, "pc1", "peca-2.gif", "peca-3.gif", "peca-4.gif", "peca-5.gif", "peca-6.gif", "peca-7.gif","peca-8.gif", this);
map_img = getImage(getCodeBase(),mapCentral.getImagem());

pc1 = new Partes("peca-1.gif","pc1",308,179,83,106, mapCentral);
pc1_img = getImage(getCodeBase(),pc1.getImagem());
pc2 = new Partes("peca-2.gif","pc2",60,174,83,106, mapCentral);
pc2_img = getImage(getCodeBase(),pc2.getImagem());
pc3 = new Partes("peca-3.gif","pc3",348,301,43,75,mapCentral);
pc3_img = getImage(getCodeBase(),pc3.getImagem());
pc4 = new Partes("peca-4.gif","pc4",182,166,86,48,mapCentral);
pc4_img = getImage(getCodeBase(),pc4.getImagem());
pc5 = new Partes("peca-5.gif","pc5",57, 300,43,75,mapCentral);
pc5_img = getImage(getCodeBase(),pc5.getImagem());
pc6 = new Partes("peca-6.gif","pc6",230,45,179,147,mapCentral);
pc6_img = getImage(getCodeBase(),pc6.getImagem());
pc7 = new Partes("peca-7.gif","pc7",53,41,152,125,mapCentral);
pc7_img = getImage(getCodeBase(),pc7.getImagem());
pc8 = new Partes("peca-8.gif","pc8",85,230,278,150,mapCentral);
pc8_img = getImage(getCodeBase(),pc8.getImagem());
btn = new Botao("botao.gif",520,350,64,48,this);
btn_img = getImage(getCodeBase(),btn.getImagem());


this.addMouseListener(this);
this.addMouseMotionListener(this);

currentState = GameState.stateStoped;

}

public void setImage(Mapeamento m){

map_img = getImage(getCodeBase(),m.getImagem());

}

public void start()
{
Thread th = new Thread(this);
th.start();
}

public void stop()
{

}

public void destroy()
{

}

public void mouseDragged(MouseEvent e)
{

}

public void mouseExited(MouseEvent e)
{

}

public void mouseMoved(MouseEvent e)
{

}

public void mouseEntered(MouseEvent e)
{

}

public void mouseClicked(MouseEvent e)
{

}

public void mouseReleased(MouseEvent e)
{

}

public void mousePressed(MouseEvent e)
{
btn.mousePressed(e);
switch(currentState)
{
case statePlaying:
{
pc1.mousePressed(e);
pc2.mousePressed(e);
pc3.mousePressed(e);
pc4.mousePressed(e);
pc5.mousePressed(e);
pc6.mousePressed(e);
pc7.mousePressed(e);
pc8.mousePressed(e);
}
break;
}
}

public void update(Graphics g)
{
if(dbImage == null)
{
dbImage = createImage(this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics();
}

dbg.setColor(getBackground());
dbg.fillRect(0,0,this.getSize().width,this.getSize().height);

dbg.setColor(getForeground());
paint(dbg);

g.drawImage(dbImage, 0, 0, this);
}

public void run()
{
//Baixa prioridade para o Thread
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

//Roda um loop infinito
while (true)
{
//Adicionar lógica aqui

repaint();
try
{
Thread.sleep(20);
}
catch (InterruptedException ex)
{
//FAZ NADA!!
}

Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}

public void paint(Graphics g)
{
//Adicionar elementos a serem pintados
g.drawImage(bGround,0,0,this);
g.drawImage(map_img,mapCentral.getX(),mapCentral.getY(),this);
g.drawImage(pc1_img,pc1.getX(),pc1.getY(),this);
g.drawImage(pc2_img,pc2.getX(),pc2.getY(),this);
g.drawImage(pc3_img,pc3.getX(),pc3.getY(),this);
g.drawImage(pc4_img,pc4.getX(),pc4.getY(),this);
g.drawImage(pc5_img,pc5.getX(),pc5.getY(),this);
g.drawImage(pc6_img,pc6.getX(),pc6.getY(),this);
g.drawImage(pc7_img,pc7.getX(),pc7.getY(),this);
g.drawImage(pc8_img,pc8.getX(),pc8.getY(),this);
g.drawImage(btn_img,btn.getX(),btn.getY(),this);


}
}
    
	